package com.glsc.ngateway.platform.controller;

import com.glsc.ngateway.platform.service.EventImproveService;
import com.glsc.ngateway.platform.service.UserService;
import com.glsc.ngateway.common.api.oaflow.feign.IFeignOaFlowService;
import com.glsc.ngateway.platform.service.gitee.GiteeService;
import com.glsc.ngateway.platform.service.itflow.schedule.PlatformSyncOaflowTask;
import com.glsc.ngateway.common.api.common.enums.DictConstant;
import com.glsc.ngateway.common.api.common.ex.Result;
import com.glsc.ngateway.platform.service.other.SystemLicenseExpireService;
import com.glsc.ngateway.platform.service.vendor.VendorRateDetailService;
import com.xxl.job.core.handler.annotation.XxlJob;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;


/**
 * @Author: libj
 * @Date: 2021/10/20
 * @Desc: 作业信息管理
 */
@RestController
@RequestMapping("/api/jobcontrol")
@Api(value = "JobControlController", tags = {"作业信息管理管理接口"})
public class JobController {

    private static Logger logger = LoggerFactory.getLogger(JobController.class);

    @Value("${job_token}")
    String jobToken;

    @Resource
    private SystemLicenseExpireService systemLicenseExpireService;

    @Resource
    private UserService userService;

    @Resource
    private EventImproveService eventImproveService;

    @Autowired
    private IFeignOaFlowService oaFlowService;

    @Autowired
    private PlatformSyncOaflowTask platformSyncOaflowTask;

    @Resource
    private VendorRateDetailService vendorRateDetailService;

    @Resource
    private GiteeService giteeService;

    /**
     * 根据传入参数不同执行不同的任务
     *
     * @param jobType
     * @return
     */
    @GetMapping("/run")
    @ApiOperation(value = "执行", notes = "执行")
    public Result run(@ApiParam(value = "任务类型") @RequestParam(required = true) String jobType,
                      @RequestParam(required = true) String token) {
        String config = jobToken;
        if (config == null || !config.equals(token)) {
            logger.error("口令未配置或口令不正确");
            return Result.failData("口令未配置或口令不正确");
        }
        String returnString = "";
        try {
            logger.info("任务类型为" + jobType);
            switch (jobType) {
                case DictConstant.JOB_NOTIFY_TIME_END_LICENSE:
                    systemLicenseExpireService.timeEndNotify();
                    break;
                case DictConstant.JOB_SYNC_OA_USER:
                    returnString = userService.updateUnLoginUser();//废弃
                    break;
                case DictConstant.NOTIFY_EVENT_IMPROVE_END:
                    returnString = eventImproveService.notifyEventImproveEnd(null);
                    break;
                case DictConstant.CHECK_SYNC_LOG:
                    oaFlowService.checkSysnLog();//未找到调用方，弃用
                    break;
                case DictConstant.SYNC_XIR_TRADE:
                    oaFlowService.syncXirTrade(null, false);//不在此处调用
                    break;
                case DictConstant.SYNC_TODO_FLOW:
                    platformSyncOaflowTask.syncTodoFlow();//已下线
                    break;
                case DictConstant.NOTIFY_VENDOR:
                    returnString = vendorRateDetailService.notifyVendor(null);//已下线
                    break;
                case DictConstant.VENDOR_CREATE_TASK:
                    vendorRateDetailService.createVendorFlowTask();//已下线
                    break;
                default:
                    return Result.failData("未匹配上任务");
            }
        } catch (Exception e) {
            return Result.failData(e.getMessage() + returnString);
        }
        return Result.successData("成功" + returnString);
    }


}
